package com.tuoyu.peopletest.adapter;//package com.stresscude.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import com.tuoyu.peopletest.R;
import com.tuoyu.peopletest.scinterface.OnItemDataListener;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class BTDevicesAdapter extends RecyclerView.Adapter<BTDevicesAdapter.MyMCHolder> {
    public String TAG = this.getClass().getSimpleName();
    public OnItemDataListener<String> mListener;

    private List<String> mDatas;
    private Context mContext;
    private boolean isOnClick;

    public BTDevicesAdapter(Context mContext, List<String> mDatas) {
        this.mDatas = mDatas;
        this.mContext = mContext;
    }

    public BTDevicesAdapter(Context mContext, List<String> mDatas, boolean isOnClick) {
        this.mDatas = mDatas;
        this.mContext = mContext;
        this.isOnClick = isOnClick;
    }


    @Override
    public MyMCHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_bt_devices, null);
        MyMCHolder holder = new MyMCHolder(itemView);

        if (isOnClick) {
            holder.itemView.setOnClickListener((v) -> {
                int position = (int) v.getTag(R.id.sc_id_recycleview_item);
                if (mListener != null) {
                    mListener.onItemData(mDatas.get(position), position);
                }

            });
        }

        return holder;
    }

    @Override
    public int getItemCount() {
        return mDatas.size();
    }

    @Override
    public void onBindViewHolder(MyMCHolder holder, int position) {
        holder.itemView.setTag(R.id.sc_id_recycleview_item, position);
        holder.tvName.setText("" + mDatas.get(position));
    }

    public void setItemClickListener(OnItemDataListener<String> listener) {
        this.mListener = listener;
    }


    public void upDatas(List<String> list) {
        mDatas.clear();
        if (list != null) {
            mDatas.addAll(list);
        }
    }

    static class MyMCHolder extends RecyclerView.ViewHolder {
        public TextView tvName;

        public MyMCHolder(@NonNull View itemView) {
            super(itemView);
            tvName = itemView.findViewById(R.id.tv_name);
        }
    }
}
